package org.dftproject.familysearch.ui;

import org.dftproject.familysearch.core.FamilySearchManager;
import org.dftproject.genesis.ui.figures.ButtonFigure;
import org.dftproject.genesis.ui.figures.EditFigure;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.figures.UniformSizeContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.dftproject.genesis.ui.pages.summary.SummaryPage;
import org.dftproject.genesis.ui.pages.summary.SummaryPageInput;
import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class ConnectPage extends AbstractPagePart {

	public static final String ID = ConnectPage.class.getName();
	
	private FigureCanvas canvas;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		setInput(input);
		setSite(site);
	}

	@Override
	public void createPartControl(Composite parent) {
		createContents(parent);
	}
	
	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);

		// Create the root figure

		Figure topContainer = new PageContainer();
		canvas.getViewport().setContentsTracksWidth(true);
		canvas.setContents(topContainer);
		
		topContainer.add(PageUtils.createH1Label("Connect to FamilySearch.org"));
		
		topContainer.add(PageUtils.createFlowedText("Enter the ID for a person on FamilySearch.org below:"));
		
		IFigure subsection = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		subsection.setLayoutManager(layout);
		subsection.setBorder(new MarginBorder(10, 10, 30, 0));
		topContainer.add(subsection);
		
		Figure container = new Figure();
		layout = new ToolbarLayout(true);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		subsection.add(container);
		
		UniformSizeGroup uniformSizeGroup = new UniformSizeGroup();
		
		Label label = new Label("Person ID:");
		label.setTextPlacement(PositionConstants.NORTH);
		label.setTextAlignment(PositionConstants.RIGHT);
		container.add(new UniformSizeContainer(label, uniformSizeGroup));
		
		final EditFigure idFigure = new EditFigure(canvas, SWT.BORDER);
		container.add(idFigure);
		
		ButtonFigure openBtn = new ButtonFigure(canvas, SWT.PUSH);
		openBtn.setText("Goto");
		container.add(openBtn);

		openBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				String id = idFigure.getText();
				
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new SummaryPageInput(FamilySearchManager.getDefault().getPerson(id)), SummaryPage.ID, true, false);
			}
			
		});
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
